<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>

    <style>
      #myInput {
        background-image: url("images/searchicon.png");
        background-position: 10px 12px; /*图片定位 */
        background-repeat: no-repeat; /* 不重复图片 */
        font-size: 16px;
        padding: 12px 20px 12px 40px;
        border: 1px solid #ddd;
        margin-bottom: 12px;
      }
      #myTable {
        border-collapse: collapse;
        width: 100%;
        border: 1px solid #ddd;
        font-size: 18px;
      }
      #myTable th,
      #myTable td {
        text-align: left;
        padding: 12px;
      }
      #myTable tr {
        /* 表格添加边框 */
        border-bottom: 1px solid #ddd;
      }
      #myTable tr.header,
      #myTable tr:hover {
        /* 表头及鼠标移动过 tr 时添加背景 */
        background-color: #f1f1f1;
      }
    </style>
  </head>

  <body>
    <input type="text" id="myInput" placeholder="搜索公司..." />
    <table id="myTable">
      <tr class="header">
        <th onclick="sortTable(0)" style="width: 60%">公司名称</th>
        <th onclick="sortTable(1)" style="width: 40%">国家</th>
      </tr>
      <tr>
        <td>Alfreds Futterkiste</td>
        <td>Germany</td>
      </tr>
      <tr>
        <td>Berglunds snabbkop</td>
        <td>Sweden</td>
      </tr>
      <tr>
        <td>Island Trading</td>
        <td>UK</td>
      </tr>
      <tr>
        <td>Koniglich Essen</td>
        <td>Germany</td>
      </tr>
    </table>
  </body>

  <script>
  function sortTable(n) {
  var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
  table = document.getElementById("myTable");
  switching = true;
  // 设置升序排列
  dir = "asc";
  /* 设置一个循环语句 */
  while (switching) {
    // 设置循环结束标记
    switching = false;
    rows = table.rows;
    /* 循环列表，从第二行开始 */
    for (i = 1; i < (rows.length - 1); i++) {
      // 设置元素是否调换位置
      shouldSwitch = false;
      /* 获取要比较的元素,
      one from current row and one from the next: */
      x = rows[i].getElementsByTagName("TD")[n];
      y = rows[i + 1].getElementsByTagName("TD")[n];
      /* 判断是否将下一个元素与当前元素进行切换 (asc 或 desc):  */
      if (dir == "asc") {
        if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
          // 设置调换元素标记，并结束当前循环
          shouldSwitch = true;
          break;
        }
      } else if (dir == "desc") {
        if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
          // 设置调换元素标记，并结束当前循环
          shouldSwitch = true;
          break;
        }
      }
    }
    if (shouldSwitch) {
      /* 如果元素调换位置设置为 true，则进行对调操作 */
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
      switching = true;
      // 每次对调完成时，将 switchcount 增加 1
      switchcount ++;
    } else {
      /* 如果完成所有元素的排序且 direction 设置为 "asc",这时就将 direction 设置为 "desc" 并再次执行循环 */
      if (switchcount == 0 && dir == "asc") {
        dir = "desc";
        switching = true;
      }
    }
  }
}

    function search() {
      let input = document.getElementById("myInput");
      let filter = input.value.toUpperCase();
      let table = document.getElementById("myTable");
      let tr = table.querySelectorAll("tr");

      // 循环表格每一行，查找匹配项
      for (let i = 0; i < tr.length; i++) {
        let td = tr[i].querySelectorAll("td")[0]; //获得第一列（公司名称）
        if (td) {
          if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
            tr[i].style.display = "";
          } else {
            tr[i].style.display = "none";
          }
        }
      }

    }
    let input = document.getElementById("myInput");
    input.addEventListener("input", search);
  </script>
</html>
